package com.ys.yscamerademo;


import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import java.io.IOException;


public class MainActivity extends AppCompatActivity implements View.OnClickListener/*, SurfaceHolder.Callback*/ {


    private final String TAG = "MainActivity";
    private Button mOpenBtn, mCaptureBtn, mSwitchBtn;

    private SurfaceView mPreviewSurface;
    private SurfaceView mPreviewSurface_1;

    private SurfaceHolder mHolder;
    private SurfaceHolder mHolder_1;
    Camera mCamera;
    Camera mCamera_1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initHolder();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initHolder() {
        mHolder = mPreviewSurface.getHolder();
        mHolder_1 = mPreviewSurface_1.getHolder();
        holderAddCallback(mHolder);
        holderAddCallback(mHolder_1);
    }

    private void initViews() {
        mOpenBtn = (Button) findViewById(R.id.open_btn);
        mOpenBtn.setOnClickListener(this);

        mCaptureBtn = (Button) findViewById(R.id.capture_btn);
        mCaptureBtn.setOnClickListener(this);

        mSwitchBtn = (Button) findViewById(R.id.switch_btn);
        mSwitchBtn.setOnClickListener(this);

        mPreviewSurface = (SurfaceView) findViewById(R.id.preview_suf);
        mPreviewSurface_1 = (SurfaceView) findViewById(R.id.preview_suf_1);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.open_btn:
                if (mCamera == null) {
                    openCamera(0);
                    Button btn = (Button) view;
                    btn.setText(R.string.close_camera_0);
                } else {
                    closeCamera(0);
                    Button btn = (Button) view;
                    btn.setText(R.string.open_camera_0);
                }

                break;
            case R.id.capture_btn:
                if (mCamera_1 == null) {
                    openCamera(1);
                    Button btn = (Button) view;
                    btn.setText(R.string.close_camera_1);
                } else {
                    closeCamera(1);
                    Button btn = (Button) view;
                    btn.setText(R.string.open_camera_1);
                }
                break;
            case R.id.switch_btn:
                break;
            default:
                break;
        }
    }

    // 打开摄像头预览
    private void holderAddCallback(SurfaceHolder holder) {
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            }

            @Override
            public void surfaceCreated(SurfaceHolder arg0) {
            }
        });

    }

    private void openCamera(int cameraID) {
        Camera mcamera;

        if (cameraID == 0) {
            mcamera = mCamera;
        } else {
            mcamera = mCamera_1;
        }
        if (mcamera != null) {
            Log.e(TAG, "camera is open");
            return;
        }

        mcamera = Camera.open(cameraID);
        try {
            if (cameraID == 0) {
                mcamera.setPreviewDisplay(mHolder);
            } else {
                mcamera.setPreviewDisplay(mHolder_1);
            }
//            Camera.Parameters parameters = mcamera.getParameters();
//            parameters.setPreviewSize(320, 240);
//            parameters.setPictureSize(320, 240);
//            mcamera.setParameters(parameters);
            mcamera.startPreview();
            if (cameraID == 0) {
                mCamera = mcamera;
            } else {
                mCamera_1 = mcamera;
            }
            return;
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }

    }

    private void closeCamera(int cameraID) {
        Camera mcamera;
        if (cameraID == 0) {
            mcamera = mCamera;
        } else {
            mcamera = mCamera_1;
        }
        if (mcamera == null) {
            Log.e(TAG, "camera is close");
            return;
        }
        if (mcamera != null) {
            mcamera.setPreviewCallback(null);
            mcamera.stopPreview();// 停掉原来摄像头的预览
            mcamera.release();
            mcamera = null;
        }
        if (cameraID == 0) {
            mCamera = mcamera;
        } else {
            mCamera_1 = mcamera;
        }
    }




    @Override
    protected void onPause() {
        super.onPause();
    }
}
